package org.csu.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.mypetstore.commonResponse.CommonResponse;
import org.csu.mypetstore.domin.Cart;
import org.csu.mypetstore.persistence.CartMapper;
import org.csu.mypetstore.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("CartService")
public class CartServiceImpl implements CartService {

    @Autowired
    CartMapper cartMapper;
    //通过username获得购物车列表
    @Override
    public CommonResponse<List<Cart>> getCartList(String username) {
        QueryWrapper<Cart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<Cart> cartList=cartMapper.selectList(queryWrapper);
        if(cartList==null){
            return CommonResponse.createForError("通过username查找所有购物车失败");
        }
        return CommonResponse.createForSuccess("查找成功",cartList);
    }

    //获得购物车
    @Override
    public CommonResponse<Cart> getCart(String itemId, String username) {
        QueryWrapper<Cart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username).eq("itemId",itemId);
        Cart cart=cartMapper.selectOne(queryWrapper);
        if(cart==null){
            return CommonResponse.createForError("通过itemId查找购物车失败");
        }
        return CommonResponse.createForSuccess("查找成功",cart);
    }

    //添加购物车
    @Override
    public CommonResponse<Cart> InsterToCart(Cart cart) {
        CommonResponse<Cart> commonResponse=getCart(cart.getItemId(), cart.getUsername());
        if(commonResponse.getStatus()==1){
            Cart cart1=commonResponse.getData();
            cart1.setNum(cart1.getNum()+cart.getNum());
            UpdateCart(cart1);
            return CommonResponse.createForSuccess("该物品已存在于购物车，num增加成功",cart1);
        }else {
            int x=cartMapper.insert(cart);
            if(x==0){
                return CommonResponse.createForError("增加购物车失败");
            }
            return CommonResponse.createForSuccess("增加成功",cart);
        }

    }

    //删除购物车
    @Override
    public CommonResponse<String> DeleteCart(String itemId, String username) {
        QueryWrapper<Cart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("itemId",itemId).eq("username",username);
        int x=cartMapper.delete(queryWrapper);
        if(x==0){
            return CommonResponse.createForError("删除购物车失败");
        }
        return CommonResponse.createForSuccess("删除购物车成功");
    }

    //更新购物车
    @Override
    public CommonResponse<Cart> UpdateCart(Cart cart) {
        UpdateWrapper<Cart> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("username",cart.getUsername()).eq("itemId",cart.getItemId());
        int x=cartMapper.update(cart,updateWrapper);
        if(x==0){
            return CommonResponse.createForError("修改购物车失败");
        }
        return CommonResponse.createForSuccess("修改成功",cart);

    }
}
